<?php

function setcolor($v) {
  if(!$v) return "<span>0.00</span>";
  if($v>0) return "<span class='green'>$v</span>";
  elseif($v<0) return "<span class='red'>$v</span>";
  else return "<span class='gray'>$v</span>";
}

class page_index {
  
  var $configs   = array();
  var $classes   = array();
  var $races     = array();
  var $ranks     = array();
  var $dkptypes  = array();

  function _default() {
    $this->members();
  }
  
  
  function members() {
    list($navi,$members,$raidtotal) = $this->data_members();
    $classes  = $this->classes;
    $dkptypes = $this->dkptypes;
    include T('members.html');
  }
  
  function items() {
    list($navi,$items) = $this->data_items();
    include T('items.html');
  }
  
  function events() {
    list($navi,$events) = $this->data_events();
    include T('events.html');
  }
  
  function news() {
    list($navi,$news) = $this->data_news();
    include T('news.html');
  }
  
  function search() {
    global $db;
    $name = trim($_REQUEST['name']);
    if(!$name) redirect('',1,'关键字不能为空');
    $sql = "SELECT  
      a.member_id AS member_id,
      a.parent_id AS parent_id,
      a.member_name AS member_name,
      a.member_level AS member_level,
      a.firstraid AS firstraid,
      a.lastraid AS lastraid,
      a.raidcount AS raidcount,
      b.race_name AS member_race,
      b.race_color AS race_color,
      b.race_pic AS race_pic,
      b.race_en AS race_en,
      c.class_name AS member_class,
      c.class_color AS class_color,
      c.class_pic AS class_pic,
      c.class_en AS class_en,
      d.rank_name AS member_rank      
      FROM @_@members AS a
      LEFT JOIN @_@races AS b ON b.race_id=a.member_race
      LEFT JOIN @_@classes AS c ON c.class_id=a.member_class     
      LEFT JOIN @_@ranks AS d ON d.rank_id=a.member_rank
      WHERE a.member_name LIKE '%$name%'";
    $membersLIST = $db->getAll($sql);    
    include T('search.html');  
  }
  
  
  function news_details() {
    global $db;
    $id = (int)$_REQUEST['id'];
    $data = $db->getOne("SELECT * FROM @_@articles WHERE article_id=$id");
    include T('news_details.html');
  }
  
  function event_details() {
    global $db;
    $id = (int)$_REQUEST['id'];
    $classes = $this->classes;
    if($data = $db->getOne("SELECT * FROM @_@events WHERE event_id=$id")) {
      $jsonFile = C('Cache')->get('cache_dir') . $data['event_id'].'.json';
      if(file_exists($jsonFile)) {
        $json  = file_get_contents($jsonFile);
        $event = json_decode($json,true);
      }
      else {
        $event = array();
      }
      include T('event_details.html');
    }
  }
  
  function member_details() {
    global $db;
    $id = (int)$_REQUEST['id'];
    $memberBASE = $db->getOne("SELECT  
      a.member_id AS member_id,
      a.parent_id AS parent_id,
      a.member_name AS member_name,
      a.member_level AS member_level,
      a.firstraid AS firstraid,
      a.lastraid AS lastraid,
      a.raidcount AS raidcount,
      b.race_name AS member_race,
      b.race_color AS race_color,
      b.race_pic AS race_pic,
      b.race_en AS race_en,
      c.class_name AS member_class,
      c.class_color AS class_color,
      c.class_pic AS class_pic,
      c.class_en AS class_en,
      d.rank_name AS member_rank      
      FROM @_@members AS a
      LEFT JOIN @_@races AS b ON b.race_id=a.member_race
      LEFT JOIN @_@classes AS c ON c.class_id=a.member_class     
      LEFT JOIN @_@ranks AS d ON d.rank_id=a.member_rank
      WHERE a.member_id=$id");
    
    echo arr_dump($memberBASE);
  }
  
  
  function __call($key,$val) {
    $this->members();
  }
  
  function __construct() {
    global $db;
    $this->configs  = $db->getAll("SELECT * FROM @_@configs");
    $this->classes  = $db->getAll("SELECT * FROM @_@classes");
    $this->races    = $db->getAll("SELECT * FROM @_@races");
    $this->ranks    = $db->getAll("SELECT * FROM @_@ranks");
    $this->dkptypes = $db->getAll("SELECT * FROM @_@dkptype");
  }
 
  function data_members() {
    global $db;
    $sql = "SELECT
    a.type_id AS type_id,
    a.member_id AS member_id,
    a.dkp_earn AS dkp_earn,
    a.dkp_spent AS dkp_spent,
    a.dkp_adjust AS dkp_adjust,
    a.dkp_base AS dkp_base,
    b.member_name AS member_name,
    b.member_level AS member_level,
    b.raidcount AS raidcount,
    b.firstraid AS firstraid,
    b.lastraid AS lastraid,
    c.race_name AS member_race,
    c.race_color AS race_color,
    c.race_pic AS race_pic,
    c.race_en AS race_en,
    d.class_name AS member_class,
    d.class_color AS class_color,
    d.class_pic AS class_pic,
    d.class_en AS class_en,
    e.rank_name AS member_rank
    FROM @_@dkplist AS a
    LEFT JOIN @_@members AS b ON b.member_id=a.member_id
    LEFT JOIN @_@races AS c ON c.race_id=b.member_race
    LEFT JOIN @_@classes AS d ON d.class_id=b.member_class 
    LEFT JOIN @_@ranks AS e ON e.rank_id=b.member_rank ";
    
    $req  = C('Url')->parser();
    $page = $req['params']['page'] ? $req['params']['page'] : $_REQUEST['page'];
    $size = $req['params']['size'] ? $req['params']['size'] : $_REQUEST['size'];
    
    $typeid  = (int)$_REQUEST['typeid'];  //DKP类型
    $classid = (int)$_REQUEST['classid']; //职业
    
    if($classid) $condi= " b.member_class=$classid ";
    else $condi = ' 1=1 ';
    
    if($typeid!=0) {
      $pids = "$typeid,";
      $rez = $db->getAll("SELECT type_id FROM @_@dkptype WHERE type_path LIKE '%$pids%'"); //所有子类统计
      foreach($rez as $_=>$v) $pids .= $v['type_id'] . ',';
      $pids = str_replace("0,","",substr($pids,0,-1));
      $raidtotal = count($db->getAll("SELECT event_id FROM @_@events WHERE type_id IN ($pids)")); //该DKP下的总raid次数
      if($size==-1) {       
       $data = $db->getAll($sql." WHERE $condi AND a.type_id IN ($pids)");
       $navi = array('pages'=>1,'page'=>1,'total'=>count($data));
      }
      else {
        list($navi,$data) = $db->pagination($sql." WHERE $condi AND a.type_id IN ($pids)",$page,$size);
      }
      return array($navi,$data,$raidtotal);
    }
    else {
      return array(array(),array(),1);
    }
  }
  
  function data_items() {
    global $db;
    $sql = "SELECT 
    a.event_id AS event_id,
    a.item_id AS item_id,
    a.item_name AS item_name,
    a.item_color AS item_color,
    a.item_looter AS item_looter,
    a.item_score AS item_score,
    a.item_count AS item_count,
    a.loot_time AS loot_time,
    b.event_name AS event_name 
    FROM @_@items AS a
    LEFT JOIN @_@events AS b ON b.event_id=a.event_id ";
    $req  = C('Url')->parser();
    $page = $req['params']['page'] ? $req['params']['page'] : $_REQUEST['page'];
    $size = $req['params']['size'] ? $req['params']['size'] : $_REQUEST['size'];
    return $db->pagination($sql,$page,$size);
  }
  
  function data_events() {
    global $db;
    $sql = "SELECT 
    b.type_name AS type_name,
    c.instance_name AS instance_name,
    a.event_id AS event_id,
    a.event_name AS event_name,
    a.event_start AS event_start,
    a.event_end AS event_end,
    a.event_note AS event_note,
    a.posttime AS posttime 
    FROM @_@events AS a
    LEFT JOIN @_@dkptype AS b ON b.type_id=a.type_id 
    LEFT JOIN @_@instances AS c ON c.instance_id=a.instance_id 
    ORDER BY a.event_id DESC ";
    $req  = C('Url')->parser();
    $page = $req['params']['page'] ? $req['params']['page'] : $_REQUEST['page'];
    $size = $req['params']['size'] ? $req['params']['size'] : $_REQUEST['size'];
    return $db->pagination($sql,$page,$size);
  }
  
  function data_news() {
    global $db;
    $sql = "SELECT * 
    FROM @_@articles 
    ORDER BY article_rank DESC, article_id DESC ";
    $req  = C('Url')->parser();
    $page = $req['params']['page'] ? $req['params']['page'] : $_REQUEST['page'];
    $size = $req['params']['size'] ? $req['params']['size'] : $_REQUEST['size'];
    return $db->pagination($sql,$page,$size);
  }
  
  function data_instances() {
    global $db;
    return $db->getAll("SELECT * FROM @_@instances");
  }
  
  function data_bosses() {
    global $db;
    return $db->getAll("SELECT * FROM @_@bosses");
  }
  
  function data_growing() {
    global $db;
    $typeid   = (int)$_REQUEST['typeid'];
    $memberid = (int)$_REQUEST['memberid'];
    if(!$memberid) return array();
    $sql = "SELECT
    a.type_id AS type_id,
    a.member_id AS member_id,
    a.event_id AS event_id,
    a.earn AS earn,
    a.spent AS spent,
    b.type_name AS type_name,
    c.event_name AS event_name,
    c.event_start AS event_start
    FROM @_@growing AS a
    LEFT JOIN @_@dkptype AS b ON b.type_id=a.type_id
    LEFT JOIN @_@events AS c ON c.event_id=a.event_id 
    WHERE a.member_id=$memberid ";
    $req  = C('Url')->parser();
    $page = $req['params']['page'] ? $req['params']['page'] : $_REQUEST['page'];
    $size = $req['params']['size'] ? $req['params']['size'] : $_REQUEST['size'];
    if($typeid!=0) {
      $pids = "$typeid,";
      $rez = $db->getAll("SELECT type_id FROM @_@dkptype WHERE type_path LIKE '%$pids%'");
      foreach($rez as $_=>$v) $pids .= $v['type_id'] . ',';
      $pids = str_replace("0,","",substr($pids,0,-1));
      $raidtotal = count($db->getAll("SELECT event_id FROM @_@events WHERE type_id IN ($pids)"));
      return $db->pagination($sql." AND a.type_id IN ($pids)",$page,$size);
    }
  }
  
  function armory() {
    $req  = C('Url')->parser();
    $str  = $req['params']['armory'] ? $req['params']['armory'] : $_REQUEST['armory'];
    list($type,$idORealm,$name) = explode(',',$str);
    switch($type) {
      case 'item':
        echo C('Armory')->get_item($idORealm);
        break;
      case 'armory':
        return C('Armory')->get_armory($idORealm,$name);
        break;
      default:
        echo('error:armory');
    }
  }
  
}

?>